package com.js.nickcar.module.addcar.uploadphoto;

import com.js.nickcar.api.HttpHelpter;
import com.js.nickcar.api.UrlConfig;
import com.js.nickcar.api.UrlUtils;
import com.js.nickcar.base.ApiCallback;
import com.js.nickcar.base.BasePresenter;
import com.js.nickcar.base.HttpResultFunc;
import com.js.nickcar.entity.local.UploadCarPhotoBean;
import com.js.nickcar.entity.local.UploadPhotoBean;
import com.js.nickcar.entity.remote.UploadImageBean;
import com.zyzd.commontools.log.TLog;

import java.io.File;
import java.util.ArrayList;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by LZY on 2017/10/7.
 */
public class UploadCarPhotoPresenter extends BasePresenter<UploadCarPhotoView> {
    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    public String mCarId;

    public void setCarId(String carId) {
        mCarId = carId;
    }

    public void uploadImage(UploadPhotoBean uploadPhotoBean) {
        uploadPhotoBean.setState(UploadPhotoBean.STATE_UPLOADING);

        MultipartBody.Builder builder = new MultipartBody.Builder();
        File file = new File(uploadPhotoBean.getLocalPath());
        builder.setType(MultipartBody.FORM);
        RequestBody requestBody = RequestBody.create(MEDIA_TYPE_PNG, file);
        builder.addFormDataPart("image", file.getName(), requestBody);
        Observable<UploadImageBean> observable = HttpHelpter.getApiService().uploadImage(builder.build())
                .map(new HttpResultFunc<UploadImageBean>());
        addSubscription(observable, getUploadImageSubscriber(uploadPhotoBean));

    }

    private Subscriber<UploadImageBean> getUploadImageSubscriber(final UploadPhotoBean uploadPhotoBean) {
        return new ApiCallback<UploadImageBean>() {
            @Override
            public void onSuccess(UploadImageBean uploadImageBean) {
                TLog.d("zyzd", "UploadCarPhotoPresenter>>onSuccess--> ");
                uploadPhotoBean.setRemotePath(uploadImageBean.getUrl());
                uploadPhotoBean.setMd5(uploadImageBean.getMd5());
                mView.uploadImageSuccess(uploadPhotoBean);
            }

            @Override
            public void onFailure(String msg) {
                TLog.d("zyzd", "UploadCarPhotoPresenter>>onFailure--> ");
            }

            @Override
            public void onFinish() {
                TLog.d("zyzd", "UploadCarPhotoPresenter>>onFinish--> ");
            }
        };
    }

    public void commitCarImagesInfo(ArrayList<UploadCarPhotoBean> items) {
        boolean isOk = checkImagesInfo(items);
        if (!isOk) return;
        Map<String, String> requestMap = UrlUtils.getBaseRequestMap(UrlConfig.API_APPRAISER_CAR_IMAGE);
        requestMap.put("car_id", mCarId);
        for (UploadCarPhotoBean item : items) {
            ArrayList<UploadPhotoBean> uploadPhotos = item.getUploadPhotos();
            StringBuilder sb = new StringBuilder();
            for (UploadPhotoBean uploadPhoto : uploadPhotos) {
                sb.append(uploadPhoto.getMd5()).append("|");
            }
            requestMap.put(item.getType(), sb.toString());
        }
        Observable observable = HttpHelpter.getApiService().commitCarImage(requestMap)
                .map(new HttpResultFunc());
        addSubscription(observable, getCommitCarImagesSubscriber());
    }

    /**
     * 提交车辆图片信息
     *
     * @return
     */
    private Subscriber getCommitCarImagesSubscriber() {
        return new ApiCallback() {
            @Override
            public void onSuccess(Object model) {
                TLog.d("zyzd", "UploadCarPhotoPresenter>>onSuccess--> ");
                mView.commitCarImagesSuccess(mCarId);
            }

            @Override
            public void onFailure(String msg) {

            }

            @Override
            public void onFinish() {

            }
        };
    }

    public boolean checkImagesInfo(ArrayList<UploadCarPhotoBean> items) {
        for (UploadCarPhotoBean item : items) {
//            if (item.getUploadPhotos().size() == 0) {
//                showToast("请上传" + item.getTitle() + "的相关照片");
//                return false;
//            }
            if (item.getUploadPhotos().size() > 0) return true;
        }
        return false;
    }
}
